package com.jf.utils;

import java.lang.reflect.Field;

import javax.crypto.spec.IvParameterSpec;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.opengl.Visibility;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;


/**
 * 
 * @author zjf
 * 
 */
public class LogoWindow implements OnTouchListener{

	private static WindowManager wm;
	private static WindowManager.LayoutParams params;
	Context con;
	public boolean isadd = false;

	static Context mcontext;

	private static LogoWindow mLogowindow;


	private static View myview;

	public static LogoWindow getInstants(Context mcontext,View view) {
		
		
		if (mLogowindow == null) {

			mLogowindow = new LogoWindow(mcontext,view);

		}
		// mLogowindow.mactivity
		return mLogowindow;

	}

	private LogoWindow(Context co,View view) {

		mcontext = co;
		myview =view;
		createView();

	}



	private int myviewsize;

	private int screenHeigh;
	@SuppressLint("WrongConstant")
	private void createView() {

		//获取屏幕宽高
		 screenHeigh = getScreenheight(mcontext);
		//screenwith = getScreenWith(mactivity);

		wm = ((WindowManager) mcontext.getSystemService("window"));
		if (myview == null) {

			myview.setOnTouchListener(this);

		}

		params = new WindowManager.LayoutParams();
		params.format = PixelFormat.RGBA_8888;
		params.type = 1000;
		params.flags = 40;
		// params.flags |= 262144;
		// params.flags |= 512;
		params.gravity = 83;
		params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
		params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		params.alpha = (float) 1;

		params.x = 0;

		params.y = machSize(700);




	}



	private int mTouchStartX, mTouchStartY, mStartX, mStartY, mTouchCurrentX, mTouchCurrentY;

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mTouchStartX = (int) event.getRawX();
				mTouchStartY = (int) event.getRawY();
				mStartX = (int) event.getX();
				mStartY = (int) event.getY();
				break;
			case MotionEvent.ACTION_MOVE:
				mTouchCurrentX = (int) event.getRawX();
				mTouchCurrentY = (int) event.getRawY();
				params.x += mTouchCurrentX - mTouchStartX;
				params.y += mTouchCurrentY - mTouchStartY;
				wm.updateViewLayout(myview, params);

				mTouchStartX = mTouchCurrentX;
				mTouchStartY = mTouchCurrentY;
		}
		return true;
	}


	// 添加
	private static void addView() {

		wm.addView(myview, params);
		hasview = true;
	}

	private void onClick(int i) {
		// TODO Auto-generated method stub
		// if (id == ResourceUtil.getId(mContext, "iv_floating_icon")) {
		// YayaWan.stop(mactivity);
		// 打开选择窗口
		// DgameSdk.accountManager(mactivity,i);
		 
		// mhandler.sendEmptyMessageDelayed(523, 3000);
	}

	public void start() {
		// 停止摇一摇监听
	//	Yayalog.loger("开始监听暂停");
		//shakeListener.stop();
		// System.out.println("1");
		
		
	//	mhandler.sendEmptyMessageAtTime(521, 1500);

	}

	@SuppressLint("NewApi") public static int getScreenheight(Context mactivity){
		WindowManager windowManager = (WindowManager)mactivity.getSystemService(Context.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		Point outPoint = new Point();
		if (Build.VERSION.SDK_INT >= 19) {
			// 可能有虚拟按键的情况
			display.getRealSize(outPoint);
		} else {
			// 不可能有虚拟按键
			display.getSize(outPoint);
		}
		int mRealSizeWidth;//手机屏幕真实宽度
		int mRealSizeHeight;//手机屏幕真实高度
		mRealSizeHeight = outPoint.y;
		mRealSizeWidth = outPoint.x;
		return mRealSizeHeight;

	}
	
	@SuppressLint("NewApi") public static int getScreenWith(Activity mactivity){
		WindowManager windowManager = (WindowManager) mactivity.getApplication().getSystemService(Context.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		Point outPoint = new Point();
		if (Build.VERSION.SDK_INT >= 19) {
			// 可能有虚拟按键的情况
			display.getRealSize(outPoint);
		} else {
			// 不可能有虚拟按键
			display.getSize(outPoint);
		}
		int mRealSizeWidth;//手机屏幕真实宽度
		int mRealSizeHeight;//手机屏幕真实高度
		mRealSizeHeight = outPoint.y;
		mRealSizeWidth = outPoint.x;
		return mRealSizeWidth;

	}
	
	
	
	public static boolean hasview = false;

	public void Stop() {
		// 开始摇一摇监听
	//	shakeListener.start();

	}

	/**
	 * 将720像素转成其他像素值
	 * 
	 * @param size
	 * @return
	 */
	private int machSize(int size) {

	//	int dealWihtSize = DisplayUtils.dealWihtSize(size, mactivity);

		return size;
	}

	/**
	 * 获取某个view的x位置
	 * 
	 * @param view
	 * @return
	 */
	public int getViewX(View view) {
		int[] locations = new int[2];
		view.getLocationOnScreen(locations);
		int kx = locations[0];// 获取组件当前位置的横坐标
		return kx;
	}

	
	
	
}
